package com.example.teadoctor;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

//这个代码里使用了 `Intent.ACTION_SENDTO` 和 `Uri.fromParts` 来以邮件形式发送信息。在运行这个应用时，系统会展示一个选择邮件应用的对话框，用户可以从中选择要用来发送邮件的应用。
public class Opinions extends AppCompatActivity {

    EditText editTextFeedback;
    Button buttonSubmit;
    String feedback;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_opinions);
        editTextFeedback = findViewById(R.id.editTextFeedback);
        buttonSubmit = findViewById(R.id.buttonSubmit);
        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                feedback = editTextFeedback.getText().toString();
                new SubmitFeedbackTask().execute(feedback);
            }
        });
    }
        @SuppressLint("StaticFieldLeak")
        private class SubmitFeedbackTask extends AsyncTask<String, Void, Void> {

            @Override
            protected Void doInBackground(String... strings) {
                // 创建一个请求队列
                RequestQueue queue = Volley.newRequestQueue(Opinions.this);
                editTextFeedback = findViewById(R.id.editTextFeedback);
                feedback = editTextFeedback.getText().toString();
                // 创建一个POST请求
                String url = "http://43.138.141.94:8082/feedback";
                StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                // 成功响应，反馈信息存储在云服务器上的C路径，:\\Users\\Administrator\\Desktop\\用户意见反馈\\feedback.txt
                                Toast.makeText(Opinions.this, "感谢你的反馈", Toast.LENGTH_SHORT).show();
                                editTextFeedback.setText("");
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // 响应失败
                        Toast.makeText(Opinions.this, "反馈发生错误，请稍后重试！", Toast.LENGTH_SHORT).show();
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        // 添加请求参数
                        Map<String, String> params = new HashMap<>();
                        params.put("feedback", feedback);
                        return params;
                    }
                };

                // 将请求加入队列
                queue.add(stringRequest);
                return null;
            }


        }
    }



